package com.ccloud.blog.modules.music.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ccloud.blog.modules.music.domain.Music;
import com.ccloud.blog.modules.music.service.IMusicService;
import com.ccloud.common.core.reqres.response.R;
import com.ccloud.common.core.utils.Pair;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author chentailai
 * @since 2019-04-19
 */
@Slf4j
@RestController
@RequestMapping(value = "/music")
public class MusicController {

    @Value("${echo-music.defaultCover}")
    private String defaultCover;

    @Resource
    private IMusicService musicService;

    @RequestMapping(value = "/getMusicList", method = RequestMethod.POST)
    public R getMusicList(Music music) {

        if (music == null) {
            return R.fail("error");
        }
        LambdaQueryWrapper<Music> wrapper = new LambdaQueryWrapper<Music>();
        wrapper.eq(Music::getCreateBy, music.getCreateBy());
        List<Music> musicList = musicService.list(wrapper);
        return R.ok(musicList);
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public R uploadMusic(@RequestParam(value = "file") MultipartFile[] fileArr) {
        //todo 重写上传音乐文件逻辑
//        for (MultipartFile file : fileArr) {
//            Pair<String, String> filePair = FileUtil.uploadFile(file, "music");
//            if (filePair == null) {
//                return ResponseData.error("文件信息为空");
//            }
//            String fileName = null;
//            fileName = filePair.first;
//            String musicName = "";
//            String artistName = "";
//            String theme = "#" + ValueUtils.getRandColorCode();
//            if (fileName == null) {
//                return ResponseData.error("文件名为空");
//            }
//            if (fileName.contains(ConstantUtils.Public.MINUS)) {
//                //视为网易云音乐的导出格式
//                musicName = ValueUtils.formatterFileName(fileName.split(ConstantUtils.Public.MINUS)[1].trim());
//                artistName = ValueUtils.formatterFileName(fileName.split(ConstantUtils.Public.MINUS)[0].trim());
//            } else {
//                musicName = fileName;
//                artistName = "神秘人";
//            }
//            String url = filePair.second;
//            //入库
//            Music music = new Music(musicName, artistName, url, defaultCover, "", theme, RequestHolder.getCurrentUser().getId());
//            musicService.save(music);
//        }
        return R.fail();
    }
}

